#导入相应的模块
import pygame
from pygame.locals import *
import time,random

class HeroPlane:
	'''玩家飞机'''

	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.img = pygame.image.load("./images/me.png")
		self.gameover = pygame.image.load("./images/gameover.png")
		self.bullet_list = [] #存放玩家的子弹列表
		self.life = 15 #设置玩家为15条命

	def display(self):
		#绘制子弹
		for b in self.bullet_list:
			b.display()
			#b.move()
			if b.move():
				self.bullet_list.remove(b)

		#绘制玩家飞机
		self.screen.blit(self.img,(self.x,self.y))

	def move_left(self):
		'''左移飞机'''
		self.x -=5
		if self.x <= 0:
			self.x = 0

	def move_right(self):
		'''右移飞机'''
		self.x +=5
		if self.x >=400 :
			self.x = 400

	def fire(self):
		'''飞机开火'''
		self.bullet_list.append(MeBullet(self.screen,self.x,self.y))
		print(len(self.bullet_list))

	def burst(self):
		'''飞机爆炸'''
		print("飞机被击中")
		self.life -= 1
		print("剩余命：%d条"%self.life)
		if self.life == 0:
			self.screen.blit(self.gameover,(0,0))
			while True:
				pygame.display.update()
				for event in pygame.event.get():
					if event.type == pygame.QUIT:
						pygame.quit()
						exit()

	def drawTwxt(self):
		'''绘制玩家命条数'''
		pygame.font.init()
		self.font = pygame.font.SysFont('arial',16)
		self.text_surface = self.font.render("lives = %d"%self.life,True,(0,0,0),(0,255,127))
		self.textRect = self.text_surface.get_rect()
		self.textRect.center = (475,20)
		return self.text_surface,self.textRect

class MeBullet:
	'''玩家飞机的子弹类'''

	def __init__(self,screen_temp,x,y):
		self.x = x + 50
		self.y = y
		self.screen = screen_temp
		self.img = pygame.image.load("./images/me_pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.y -=20
		if self.y <= -20:
			return True

class EnemyPlane:
	'''敌机类'''

	def __init__(self,screen_temp):
		self.x = random.choice(range(0,391))
		self.y = -75
		self.screen = screen_temp
		self.img = pygame.image.load("./images/en.png")
		self.bullet_list = [] #存放敌机子弹列表

	def display(self,hero):
		'''绘制敌机'''
		#绘制子弹
		for b in self.bullet_list:
			b.display()
			# b.move()
			#对敌机子弹的回收不会做，不知道做的对不对
			mo = b.move()
			#敌机子弹与英雄飞机进行碰撞检测
			if b.x > hero.x and b.x <hero.x + 110 and b.y >= hero.y and b.y <= hero.y + 2:
				#英雄飞机爆炸效果
				for bu in range(1,5):
					self.image = pygame.image.load("./images/burst"+str(bu)+".png")
					self.screen.blit(self.image,(b.x,b.y))
					time.sleep(0.01)
				#英雄飞机减命
				hero.burst()
				return True	
			if mo is True :
				self.bullet_list.remove(b)
		#绘制敌机
		self.screen.blit(self.img,(self.x,self.y))

	def fire(self):
		self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) #调用该函数之后，将子弹加进列表，之后调用display()函数移动子弹
		print("敌机子弹数：%d"%len(self.bullet_list))

	def move(self,hero):
		self.y +=4
		#敌机出屏幕
		if self.y >580:
			return True
		#遍历所有子弹,并进行碰撞检测
		for bo in hero.bullet_list:
			if bo.x > self.x and bo.x <self.x + 117 and bo.y>self.y+10 and bo.y<self.y+50:
				#敌机爆炸效果
				for bu in range(1,5):
					self.image = pygame.image.load("./images/burst"+str(bu)+".png")
					self.screen.blit(self.image,(bo.x,bo.y))
					#print(bu)
					time.sleep(0.01)
				#玩家子弹消失
				hero.bullet_list.remove(bo)
				return True

class EnemyBullet:
	'''敌机的子弹类'''

	def __init__(self,screen_temp,x,y):
		self.x = x + 56
		self.y = y
		self.screen = screen_temp
		self.img = pygame.image.load("./images/en_pd.png")

	def display(self):
		'''绘制子弹'''
		if random.choice(range(100)) == 2:
			self.screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.y +=4
		if self.y >= 580:
			return True

def key_control(hero_temp):
	'''键盘控制函数'''
	#执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()
	# 获取键盘信息
	pressed_keys = pygame.key.get_pressed()
	# 做判断，并执行对应的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		hero_temp.move_left()
		print("left...")
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		hero_temp.move_right()
		print("right...")
	if pressed_keys[K_SPACE]:
		hero_temp.fire()
		print("space...")

def main():
	'''主程序函数'''
	#创建游戏窗口
	screen = pygame.display.set_mode((512,580),0,0) #512是窗口的宽度，是根据背景图片的宽度来的
	#设置窗口标题
	pygame.display.set_caption("飞机大战")
	#创建一个游戏背景
	background = pygame.image.load("./images/bj01.jpg")
	#创建英雄飞机（实例化）
	hero =HeroPlane(screen)
	enemylist = [] #存放敌机列表

	m = -1190
	while True:

		#绘制背景
		screen.blit(background,(0,m))
		#绘制玩家命条数
		text_surface,textRect = hero.drawTwxt()
		screen.blit(text_surface,textRect)

		m += 2
		if m>=-610:
			m = -1190

		#绘制玩家飞机
		hero.display()
		#随机绘制敌机
		if random.choice(range(50)) == 16 :
			enemylist.append(EnemyPlane(screen))
		#遍历敌机并绘制移动
		for em in enemylist:
			em.fire() #敌机开火
			em.display(hero)
			#敌机消失
			if em.move(hero):
				enemylist.remove(em)
			if em.display(hero):
				hero.burst()
				break
		#执行键盘控制
		key_control(hero)
		#更新显示
		pygame.display.update()
		#定时显示
		time.sleep(0.05)

#判断当前是否是运行主程序，并调用
if __name__ == "__main__":
	main()